var dns = require('dns'),
  querystring = require('querystring');

exports.parseDns = parseDns;

function parseDns(req, res) {
  var postData = '';
  req.on('data', function(chunk) {
    postData += chunk;
  });
  req.addListener('end', function() {
    var retData = getDns(postData, function(domain, addresses) {
      res.writeHead(200, { 'Content-Type': 'text/html' });
      var parsePage = "<html><head><meta http-equiv='content-type' content='text/html;charset=utf-8'/></head>" +
        "<body><div style='text-align:center'>Domain: <span style='color:red;'>" +
        domain + "</span>,IP: <span style='color:red'>" + addresses.join(',') + "</span><br><a href=\"/\">回到主页</a></div></body></html>";
      res.end(parsePage);
    });
    return;
  });
}

function getDns(postData, callback) {
  var domain = querystring.parse(postData).search_dns;
  dns.resolve(domain, function(err, addresses) {
    if (!addresses) { addresses = ['不存在域名！']; }
    callback(domain, addresses);
  });
}
